package com.tools.middle.elasticsearch;

import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch.core.search.HighlightField;
import com.tools.common.container.OwnPairs;
import com.tools.common.object.Note;
import com.tools.common.thread.Timeunit;

import java.util.Map;

/**
 * 单字段的 match 分词匹配包装类。
 * 继承父类的方法在多条件查询时不起作用（除了分词器）
 * */
@Note("单字段的 match 分词匹配包装类。" +
        "继承父类的方法在多条件查询时不起作用（除了分词器）")
public final class MatchSearch extends Search {

    @Note("字段名称")
    private String field;

    @Note("要分词匹配查询的内容")
    private String query;

    @Note("误差多少可以匹配出来")
    private int fuzziness = 3;

    /* **********************************************************************************
     *
     *          构造器
     *
     * *********************************************************************************
     * */

    public MatchSearch(String field, String query) {
        this.setField(field);
        this.setQuery(query);
    }

    public static MatchSearch of(String field, String query) {
        return new MatchSearch(field, query);
    }

    /* **********************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************
     * */

    public String getField() {
        return field;
    }

    public void setField(String field) {
        default_fieldNotEmpty(field);
        this.field = field;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        if(query == null || query.isEmpty()) throw new IllegalArgumentException("要查询的内容为空");
        this.query = query;
    }

    public int getFuzziness() {
        return fuzziness;
    }

    public String getFuzzinessString() {
        return String.valueOf(fuzziness);
    }

    public void setFuzziness(int fuzziness) {
        this.fuzziness = fuzziness;
    }

    @Override
    public String toString() {
        return "MatchSearch{" +
                "field=" + field +
                ", fuzziness=" + fuzziness +
                ", query=" + query +
                ", " + super.toString() +
                '}';
    }

    /* **********************************************************************************
     *
     *          链式调用
     *
     * *********************************************************************************
     * */

    public MatchSearch field(String field) {
        this.setField(field);
        return this;
    }

    public MatchSearch query(String query) {
        this.setQuery(query);
        return this;
    }

    public MatchSearch fuzziness(int fuzziness) {
        this.setFuzziness(fuzziness);
        return this;
    }

    @Override
    public MatchSearch indicesNames(String indicesName) {
        super.indicesNames(indicesName);
        return this;
    }

    @Override
    public MatchSearch indicesNames(String... indicesNames) {
        super.indicesNames(indicesNames);
        return this;
    }

    @Override
    public MatchSearch indicesNames(Iterable<String> indicesNames) {
        super.indicesNames(indicesNames);
        return this;
    }

    @Override
    public MatchSearch timeout(String timeout) {
        super.timeout(timeout);
        return this;
    }

    @Override
    public MatchSearch timeout(int timeout, Timeunit timeunit) {
        super.timeout(timeout, timeunit);
        return this;
    }

    @Override
    public MatchSearch from(int from) {
        super.from(from);
        return this;
    }

    @Override
    public MatchSearch size(int size) {
        super.size(size);
        return this;
    }

    @Override
    public MatchSearch analyzer(String analyzer) {
        super.analyzer(analyzer);
        return this;
    }

    @Override
    public MatchSearch sorts(String field, SortOrder order) {
        super.sorts(field, order);
        return this;
    }

    @Override
    public MatchSearch sorts(Map<String, SortOrder> fieldAndOrders) {
        super.sorts(fieldAndOrders);
        return this;
    }

    @Override
    public MatchSearch sorts(OwnPairs<String, SortOrder> fieldAndOrders) {
        super.sorts(fieldAndOrders);
        return this;
    }

    @Override
    public MatchSearch includes(String includeField) {
        super.includes(includeField);
        return this;
    }

    @Override
    public MatchSearch includes(String... includeFields) {
        super.includes(includeFields);
        return this;
    }

    @Override
    public MatchSearch includes(Iterable<String> includeFields) {
        super.includes(includeFields);
        return this;
    }

    @Override
    public MatchSearch excludes(String excludeField) {
        super.excludes(excludeField);
        return this;
    }

    @Override
    public MatchSearch excludes(String... excludeFields) {
        super.excludes(excludeFields);
        return this;
    }

    @Override
    public MatchSearch excludes(Iterable<String> excludeFields) {
        super.excludes(excludeFields);
        return this;
    }

    @Override
    public MatchSearch aggregations(String field, Aggregation agg) {
        super.aggregations(field, agg);
        return this;
    }

    @Override
    public MatchSearch aggregations(Map<String, Aggregation> aggregations) {
        super.aggregations(aggregations);
        return this;
    }

    @Override
    public MatchSearch aggregations(OwnPairs<String, Aggregation> aggregations) {
        super.aggregations(aggregations);
        return this;
    }

    @Override
    public MatchSearch highlights(String field, HighlightField highlight) {
        super.highlights(field, highlight);
        return this;
    }

    @Override
    public MatchSearch highlights(Map<String, HighlightField> highlights) {
        super.highlights(highlights);
        return this;
    }

    @Override
    public MatchSearch highlights(OwnPairs<String, HighlightField> highlights) {
        super.highlights(highlights);
        return this;
    }

    /* **********************************************************************************
     *
     *          自定义方法
     *
     * *********************************************************************************
     * */

    @Override
    public MatchSearch addIndicesName(String indicesName) {
        super.addIndicesName(indicesName);
        return this;
    }

    @Override
    public MatchSearch addIndicesNames(String... indicesNames) {
        super.addIndicesNames(indicesNames);
        return this;
    }

    @Override
    public MatchSearch addIndicesNames(Iterable<String> indicesNames) {
        super.addIndicesNames(indicesNames);
        return this;
    }

    @Override
    public MatchSearch modifyIndicesName(int index, String indicesName) {
        super.modifyIndicesName(index, indicesName);
        return this;
    }

    @Override
    public MatchSearch removeIndicesName(String indicesName) {
        super.removeIndicesName(indicesName);
        return this;
    }

    @Override
    public MatchSearch removeIndicesName(int index) {
        super.removeIndicesName(index);
        return this;
    }

    @Override
    public MatchSearch removeIndicesNames(String... indicesNames) {
        super.removeIndicesNames(indicesNames);
        return this;
    }

    @Override
    public MatchSearch removeIndicesNames(Iterable<String> indicesNames) {
        super.removeIndicesNames(indicesNames);
        return this;
    }

    @Override
    public MatchSearch addSort(String field, SortOrder order) {
        super.addSort(field, order);
        return this;
    }

    @Override
    public MatchSearch addSorts(Map<String, SortOrder> fieldAndOrders) {
        super.addSorts(fieldAndOrders);
        return this;
    }

    @Override
    public MatchSearch addSorts(OwnPairs<String, SortOrder> fieldAndOrders) {
        super.addSorts(fieldAndOrders);
        return this;
    }

    @Override
    public MatchSearch modifySort(int index, String field, SortOrder order) {
        super.modifySort(index, field, order);
        return this;
    }

    @Override
    public MatchSearch clearSorts() {
        super.clearSorts();
        return this;
    }

    @Override
    public MatchSearch removeSort(String field, SortOrder order) {
        super.removeSort(field, order);
        return this;
    }

    @Override
    public MatchSearch removeSort(int index) {
        super.removeSort(index);
        return this;
    }

    @Override
    public MatchSearch removeSorts(Map<String, SortOrder> fieldAndOrders) {
        super.removeSorts(fieldAndOrders);
        return this;
    }

    @Override
    public MatchSearch removeSorts(OwnPairs<String, SortOrder> fieldAndOrders) {
        super.removeSorts(fieldAndOrders);
        return this;
    }

    @Override
    public MatchSearch addInclude(String field) {
        super.addInclude(field);
        return this;
    }

    @Override
    public MatchSearch addIncludes(String... fields) {
        super.addIncludes(fields);
        return this;
    }

    @Override
    public MatchSearch addIncludes(Iterable<String> fields) {
        super.addIncludes(fields);
        return this;
    }

    @Override
    public MatchSearch modifyInclude(int index, String field) {
        super.modifyInclude(index, field);
        return this;
    }

    @Override
    public MatchSearch clearIncludes() {
        super.clearIncludes();
        return this;
    }

    @Override
    public MatchSearch removeInclude(String field) {
        super.removeInclude(field);
        return this;
    }

    @Override
    public MatchSearch removeInclude(int index) {
        super.removeInclude(index);
        return this;
    }

    @Override
    public MatchSearch removeIncludes(String... fields) {
        super.removeIncludes(fields);
        return this;
    }

    @Override
    public MatchSearch removeIncludes(Iterable<String> fields) {
        super.removeIncludes(fields);
        return this;
    }

    @Override
    public MatchSearch addExclude(String field) {
        super.addExclude(field);
        return this;
    }

    @Override
    public MatchSearch addExcludes(String... fields) {
        super.addExcludes(fields);
        return this;
    }

    @Override
    public MatchSearch addExcludes(Iterable<String> fields) {
        super.addExcludes(fields);
        return this;
    }

    @Override
    public MatchSearch modifyExclude(int index, String field) {
        super.modifyExclude(index, field);
        return this;
    }

    @Override
    public MatchSearch clearExcludes() {
        super.clearExcludes();
        return this;
    }

    @Override
    public MatchSearch removeExclude(String field) {
        super.removeExclude(field);
        return this;
    }

    @Override
    public MatchSearch removeExclude(int index) {
        super.removeExclude(index);
        return this;
    }

    @Override
    public MatchSearch removeExcludes(String... fields) {
        super.removeExcludes(fields);
        return this;
    }

    @Override
    public MatchSearch removeExcludes(Iterable<String> fields) {
        super.removeExcludes(fields);
        return this;
    }

    @Override
    public MatchSearch addAgg(String field, Aggregation agg) {
        super.addAgg(field, agg);
        return this;
    }

    @Override
    public MatchSearch addAggs(Map<String, Aggregation> aggs) {
        super.addAggs(aggs);
        return this;
    }

    @Override
    public MatchSearch addAggs(OwnPairs<String, Aggregation> aggs) {
        super.addAggs(aggs);
        return this;
    }

    @Override
    public MatchSearch clearAggs() {
        super.clearAggs();
        return this;
    }

    @Override
    public MatchSearch removeAgg(String field) {
        super.removeAgg(field);
        return this;
    }

    @Override
    public MatchSearch removeAgg(String... fields) {
        super.removeAgg(fields);
        return this;
    }

    @Override
    public MatchSearch removeAgg(Iterable<String> fields) {
        super.removeAgg(fields);
        return this;
    }

    @Override
    public MatchSearch addHighlight(String field, HighlightField highlight) {
        super.addHighlight(field, highlight);
        return this;
    }

    @Override
    public MatchSearch addHighlights(Map<String, HighlightField> highlights) {
        super.addHighlights(highlights);
        return this;
    }

    @Override
    public MatchSearch addHighlights(OwnPairs<String, HighlightField> highlights) {
        super.addHighlights(highlights);
        return this;
    }

    @Override
    public MatchSearch clearHighlight() {
        super.clearHighlight();
        return this;
    }

    @Override
    public MatchSearch removeHighlight(String field) {
        super.removeHighlight(field);
        return this;
    }

    @Override
    public MatchSearch removeHighlights(String... fields) {
        super.removeHighlights(fields);
        return this;
    }

    @Override
    public MatchSearch removeHighlights(Iterable<String> fields) {
        super.removeHighlights(fields);
        return this;
    }
}
